package com.my.algorithmlearning.leetcode;

import java.util.Stack;

/**
 * @ClassName Medium_155_MinStack
 * @Description 参考题解：https://leetcode.cn/problems/min-stack/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-38/
 * @Author wanghaiwei
 * @Date 2023/6/13 15:30
 */
public class Medium_155_MinStack {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(1);
        System.out.println(minStack.getMin());
    }
}

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
        }
    }

    public void pop() {
        if (stack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
